<template>
  <div>
    <el-dialog
      :visible.sync="$store.state.loginDate.dialogVisible"
      width="960px"
      :before-close="handleConfirmClose"
    >
      <div class="login-msg-wrapper">
        <div class="write-mobile" v-show="isShowMobile">
          <h3 class="title">手机号码</h3>
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
            <el-form-item prop="mobile">
              <el-input
                @focus="focusHandle"
                @blur="blurHandle"
                type="text"
                v-model="ruleForm.mobile"
                style="width: 100%; border: none"
                placeholder="请输入手机号"
              ></el-input>
              <transition name="el-zoom-in-center">
                <div v-show="show" class="transition-box"></div>
              </transition>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                :loading="loginLoading"
                style="width: 100%; border: none"
                @click="submitForm('ruleForm')"
                >获取验证码</el-button
              >
            </el-form-item>
          </el-form>
          <div class="other-login">
            <i class="icon"></i>
            <p>第三方账号登录</p>
            <p class="relieve-frozen" @click="relieveFrozen">解冻</p>
          </div>
        </div>
        <div class="write-code" v-show="isShowCode">
          <h3 class="title">
            验证码已发送至{{ ruleForm.mobile
            }}<i class="el-icon-edit" @click="rewrite"></i>
          </h3>
          <el-form
            :model="ruleForm2"
            :rules="rules"
            ref="ruleForm2"
            class="demo-ruleForm"
          >
            <el-form-item prop="code">
              <el-input
                @focus="focusHandle"
                @blur="blurHandle"
                type="text"
                v-model="ruleForm2.code"
                style="width: 100%; border: none"
                placeholder="请输入验证码"
                v-focus
              >
              </el-input>
              <span class="timer" :class="{'resend':timerNum===-1}" @click="resend">{{ timerNum != -1 ? timerNum + 's' : '重新发送' }}</span>
              <transition name="el-zoom-in-center">
                <div v-show="show" class="transition-box"></div>
              </transition>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                :loading="loginLoading"
                style="width: 100%; border: none"
                @click="onLogin('ruleForm')"
                >登录</el-button
              >
            </el-form-item>
          </el-form>
          <div class="other-login">
            <i class="icon"></i>
            <p>第三方账号登录</p>
            <p class="relieve-frozen" @click="relieveFrozen">解冻</p>
          </div>
        </div>
        <div class="agreement">
          如未注册将自动注册并默认同意<router-link to="/">《用户协议》</router-link
          >和<router-link to="/">《隐私协议》</router-link>
        </div>
      </div>
      <div class="login-code-wrapper">
        <div class="code-section">
          <div class="code-wrapper">
            <div class="code">
              <img src="../../assets/erweima/code_login_wechat.png" alt="" />
            </div>
            <p class="describe"><i class="icon"></i>微信扫一扫关注</p>
            <p class="describe">“背景114预约挂号”</p>
            <p class="describe">快速挂号</p>
          </div>
          <div class="code-wrapper">
            <div class="code">
              <img src="../../assets/erweima/code_home.png" alt="" />
            </div>
            <p class="describe">扫一扫下载</p>
            <p class="describe">“114健康APP”</p>
          </div>
        </div>
        <div class="word-section">
          <p class="slogan">北京市卫键委官方指定平台</p>
          <p class="slogan">快速挂号 安全放心</p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Cookies from "js-cookie";

export default {
  name: "login",
  directives: {
    focus: {
      inserted: (el) => {
        el.children[0].focus();
      },
      update: (el) => {
        el.children[0].focus();
      },
    },
  },
  data() {
    return {
      timerNum: 60,
      show: false,
      loginLoading: false,
      isShowMobile: true,
      isShowCode: false,
      returnCode: "",
      ruleForm: {
        mobile: "",
      },
      ruleForm2: {
        code: "",
      },
      rules: {
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3|5|7|8|9]\d{9}$/,
            message: "请输入11位有效手机号",
            trigger: "blur",
          },
        ],
        code: [{ required: true, message: "请输入短信验证码", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleConfirmClose(done) {
      done();
    },
    handleClose() {
      this.$store.commit("loginHandleClose");
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        // 如果验证失败，不发送请求
        if (!valid) {
          return false;
        }
        this.getCode();
        this.beginTimer();
      });
    },
    rewrite() {
      this.isShowMobile = true;
      this.isShowCode = false;
      this.$refs.ruleForm.resetFields();
      this.$refs.ruleForm2.resetFields();
    },
    resend(){
      this.getCode() 
      this.beginTimer()
      this.$refs.ruleForm2.resetFields();
    },
    beginTimer() {
      let that = this
      that.timerNum = 60
      let timer = setInterval(() => {
        that.timerNum--;
        if (that.timerNum === -1) {
          clearInterval(timer);
        }
      }, 100);
    },
    getCode() {
      this.loginLoading = true;
          this.returnCode = 6666;
          console.log(this.returnCode);
          this.loginLoading = false;
          this.isShowMobile = false;
          this.isShowCode = true;
    },
    onLogin() {
      this.$refs.ruleForm2.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.login();
      });
    },
    login() {
      this.loginLoading = true;
      setTimeout(() => {
        if (this.ruleForm2.code == this.returnCode) {
          this.loginLoading = false;
          this.isShowMobile = true;
          this.isShowCode = false;
          this.handleClose();
          this.removelocalStorage(this.ruleForm.mobile);
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm2.resetFields();
        } else {
          this.$message.error("验证码有误");
          this.loginLoading = false;
          this.$refs.ruleForm2.resetFields();
        }
      }, 500);
    },
    focusHandle() {
      this.show = true;
    },
    blurHandle() {
      this.show = false;
    },
    relieveFrozen() {
      console.log("阿巴阿巴");
    },
    removelocalStorage(token) {
      sessionStorage.setItem("TokenMobile", token);
      let userMsg = sessionStorage.getItem("TokenMobile");
      this.$store.commit("getUserMobile", userMsg);
    },
  },
};
</script>
<style lang="less" scoped>
/deep/.el-dialog {
  border-radius: 5px;
  background: #f6faff;
  height: 600px;
  .login-msg-wrapper {
    width: 440px;
    height: 480px;
    background: #ffffff;
    box-shadow: 0 0 10px #d7f1fe;
    margin: -20px 0 0 40px;
    float: left;
    padding: 80px 60px 0;
    box-sizing: border-box;
    position: relative;
    .transition-box {
      position: absolute;
      width: 100%;
      height: 1px;
      bottom: 3px;
      background-color: #4a90f1;
      box-sizing: border-box;
    }
    /deep/.el-zoom-in-center-enter-active,
    .el-zoom-in-center-leave-active {
      transition: all 0.3s ease-out;
    }
    /deep/.el-zoom-in-center-enter,
    .el-zoom-in-center-leave-active {
      opacity: 1;
      transform: scaleX(0);
    }
    .title {
      letter-spacing: 1px;
      font-weight: 700;
      color: #333;
      font-size: 16px;
      /deep/.el-icon-edit {
        color: #4490f1;
        cursor: pointer;
        margin-left: 5px;
        font-weight: bold;
      }
    }
    /deep/.el-form-item {
      position: relative;
      margin-top: 64px;
      .timer {
        position: absolute;
        right: 0px;
        color: #666;
        font-size: 15px;
        letter-spacing: 1px;
      }
      .resend{
        cursor:pointer;
        color: #4490f1;
      }
      .resend:hover{
        color:#70adff;
      }
      /deep/.el-form-item__content {
        line-height: 45px;

        /deep/.el-input {
          /deep/.el-input__inner {
            border: none;
            padding: 0;
            font-size: 16px;
            letter-spacing: 1px;
            border-bottom: 1px solid #eee;
          }
        }
        /deep/.el-button {
          font-size: 16px;
          padding: 14px 20px;
          box-sizing: border-box;
        }
        /deep/.el-button:hover {
          background: #2d81f7;
        }
      }
    }

    .other-login {
      text-align: center;

      .icon {
        cursor: pointer;
        margin-top: 22px;
        display: inline-block;
        width: 43px;
        height: 43px;
        border-radius: 50%;
        background: url("../../assets/erweima/weixin.png") no-repeat center;
      }

      p {
        color: #ccc;
        margin-top: 5px;
      }
      .relieve-frozen {
        color: #4490f1;
        cursor: pointer;
      }
      .relieve-frozen:hover {
        color: #70adff;
      }
    }
    .agreement {
      position: absolute;
      bottom: -45px;
      left: 0;
      width: 100%;
      font-size: 12px;
      text-align: center;
      a {
        color: #4490f1;
      }
      a:hover {
        color: #70adff;
      }
    }
  }
  .login-code-wrapper {
    float: left;
    width: 310px;
    overflow: hidden;
    margin: 50px 0 0 75px;
    .code-section {
      display: flex;
      justify-content: space-between;
      .code-wrapper {
        float: left;
        width: 133px;
        text-align: center;
        .code {
          width: 100%;
          margin-bottom: 25px;
          img {
            width: 100%;
          }
        }
        .describe {
          color: #333;
          line-height: 21px;
          font-size: 14px;
        }
      }
    }
    .word-section {
      text-align: center;
      margin-top: 70px;
      .slogan {
        font-size: 20px;
        font-weight: 300;
        font-style: italic;
        letter-spacing: 4px;
        color: #000;
        line-height: 44px;
      }
    }
  }
}
</style>
